package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.ActivityProps;
import com.sc.nft.entity.vo.ActivityPropsUpVO;
import com.sc.nft.entity.vo.ActivityPropsVO;
import com.sc.nft.enums.BoxTakePoolConvertScenarioEnum;
import com.sc.nft.mapper.ActivityPropsMapper;
import com.sc.nft.sup.SuperEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 活动道具卡表(sc_activity_props)数据DAO
 *
 * @author Tangzehui
 * @description
 * @since 2024-01-03 16:13:13
 */
@Slf4j
@Repository
public class ActivityPropsDao extends ServiceImpl<ActivityPropsMapper, ActivityProps> {

    public void updateActivityProps(ActivityProps activityProps) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(ActivityProps::getId, activityProps.getId());
        wrapper.set(ActivityProps::getEquityPropsId, activityProps.getEquityPropsId());
        wrapper.set(ActivityProps::getCreationContent, activityProps.getCreationContent());
        wrapper.set(ActivityProps::getFirstTitle, activityProps.getFirstTitle());
        wrapper.set(ActivityProps::getSumCount, activityProps.getSumCount());
        update(wrapper);
    }

    public Page<ActivityPropsVO> adminPage(Boolean status, String firstTitle, BoxTakePoolConvertScenarioEnum convertScenario, Long daoId, Integer pageNo, Integer pageSize) {
        return baseMapper.adminPage(new Page<>(pageNo, pageSize), status, firstTitle, Objects.nonNull(convertScenario) ? convertScenario.getCode() : null, daoId);
    }

    public Page<ActivityPropsUpVO> getUpActivityPropsList(BoxTakePoolConvertScenarioEnum convertScenario, Long daoId, Integer pageNo, Integer pageSize) {
        return baseMapper.getUpActivityPropsList(new Page<>(pageNo, pageSize), Objects.nonNull(convertScenario) ? convertScenario.getCode() : null, daoId);
    }

    public Boolean subUsableCount(Long id, Integer num) {
        return SqlHelper.retBool(baseMapper.subUsableCount(id, num));
    }

    public Boolean addExchangeCount(Long id, Integer num) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, id);
        wrapper.setSql("exchange_count = exchange_count + " + num);
        return update(wrapper);
    }

    public boolean updateStatus(Long id, Boolean status) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, id);
        wrapper.set(ActivityProps::getStatus, status);
        return update(wrapper);
    }

    public boolean updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, id);
        wrapper.set(ActivityProps::getSort, sort);
        return update(wrapper);
    }

    public boolean updateUsableCount(Long id, Integer count) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, id);
        wrapper.set(ActivityProps::getUsableCount, count);
        return update(wrapper);
    }

    public boolean updateSumCount(Long id, Integer count) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, id);
        wrapper.set(ActivityProps::getSumCount, count);
        return update(wrapper);
    }

    public List<ActivityProps> getAllUpProps() {
        LambdaQueryWrapper<ActivityProps> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SuperEntity::getIsDelete, 0);
        queryWrapper.eq(ActivityProps::getStatus, 1);
        return list(queryWrapper);
    }

    public void updateRegisterTimeById(ActivityProps activityProps, Boolean isRegisterTime, Date registerBeginTime, Date registerEndTime) {
        LambdaUpdateWrapper<ActivityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityProps::getId, activityProps.getId());
        wrapper.set(ObjectUtil.isNotNull(isRegisterTime), ActivityProps::getIsRegisterTime, isRegisterTime);
        wrapper.set(ObjectUtil.isNotNull(registerBeginTime), ActivityProps::getRegisterBeginTime, registerBeginTime);
        wrapper.set(ObjectUtil.isNotNull(registerEndTime), ActivityProps::getRegisterEndTime, registerEndTime);
        update(wrapper);
    }
}